package com.tgh.au.c5;

import java.util.concurrent.TimeUnit;
import com.tgh.au.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.KeyEvent;
import android.view.View;

public class GameView7 extends View implements Runnable {
	Bitmap qq;
	int qqWidth;
	int qqHeight;
	float angle=0.0f;
	Matrix matrix=new Matrix();
	private Paint paint;
	
	public GameView7(Context context) {
		super(context);
		setFocusable(true);
		requestFocus();
		qq=BitmapFactory.decodeResource(getResources(), R.drawable.qq);
		qqWidth=qq.getWidth();
		qqHeight=qq.getHeight();
		paint = new Paint();
		new Thread(this).start();
	}
	@Override
	protected void onDraw(Canvas canvas) {
		matrix.reset();
		matrix.setRotate(angle,(320-qqWidth)/2, qqHeight);
//		Bitmap bitmap = Bitmap.createBitmap(qq, 0, 0, qqWidth, qqHeight, matrix, true);
//		canvas.drawBitmap(bitmap, (320-qqWidth)/2, qqHeight, null);
		canvas.save();
		canvas.setMatrix(matrix);
		canvas.drawBitmap(qq, (320-qqWidth)/2, qqHeight, null);
		canvas.restore();
		paint.setColor(Color.YELLOW);
		canvas.drawOval(new RectF(20, 20, 70, 120), paint);
	}
	@Override
	public void run() {
		while (!Thread.currentThread().isInterrupted()) {
			try {
				TimeUnit.MILLISECONDS.sleep(100);
			} catch (Exception e) {
				Thread.currentThread().interrupt();
			}
			postInvalidate();
		}
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_LEFT:
			angle--;
			break;
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			angle++;
			break;
		}
		return true;
	}

}
